using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.Web;
using System.Collections.Generic;
using Mani.Common;

public partial class rpt_InternalInvoicePayment :System.Web.UI.Page 
{

    #region "Overriden Method"

    #endregion
    #region "Private Property"
    private string itemID;
    private string accountID;
    private string salesPersonId;
    private string type;
    private DateTime toAccountDate;
    private DateTime fromAccountDate;
    SqlConnection sq;
    #endregion
    #region "Private Method"

    private void fillItemNameList()
    {
       
    }
    private void checkControlsValues()
    {
         itemID = ddlItemName.SelectedValue; 
         accountID = ddlAccountName.SelectedValue;
         salesPersonId = ddlSalesPerson.SelectedValue; 
         type = ddlType.SelectedValue;
         if (String.IsNullOrEmpty(txtToAccountDate.Text))
         {
             toAccountDate = new DateTime(2060, 12, 12);
         }
         else
         {
             toAccountDate = MyPersianDate.PersianToDateTime(txtToAccountDate.Text);
         }
         if (String.IsNullOrEmpty(txtFromAccountDate.Text))
         {
             fromAccountDate = new DateTime(2003, 12, 12);
         }
         else
         {
             fromAccountDate = MyPersianDate.PersianToDateTime(txtFromAccountDate.Text);
         }
    }
    private void search()
    {
        checkControlsValues();
        try
        {
            sq = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ToString());

            SqlCommand command = new SqlCommand();
            command.CommandText = "SELECT * FROM vwInternalAccountingReport WHERE ((ItemId = @ItemId or @ItemId = -1)and (SalesPersonID = @SalesPerson or @SalesPerson = -1) and (AccountId = @AccountName or @AccountName=-1) and (Type = @Type or @Type=-1)and (CreateDate >=@FromAccountDate and CreateDate <= @ToAccountDate))";
            command.Parameters.AddWithValue("@ItemId", itemID);
            command.Parameters.AddWithValue("@SalesPerson", salesPersonId);
            command.Parameters.AddWithValue("@AccountName", accountID);
            command.Parameters.AddWithValue("@Type", type);
            command.Parameters.AddWithValue("@ToAccountDate", toAccountDate);
            command.Parameters.AddWithValue("@FromAccountDate", fromAccountDate);
            command.Connection = sq;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = command;
            DataSet ds = new DataSet();
            da.Fill(ds, "vwInternalAccountingReport");
            loadCrystallReport(ds);

        }
        catch (SqlException ex) { throw ex; }
    }
    private void loadCrystallReport(DataSet ds)
    {
        string reportname = "InternalAccountPayment";
        CrystalReportSource1.ReportDocument.FileName = Server.MapPath(reportname+".rpt");
        CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables[0]);
        crvInternalAccountPayment.ReportSource = CrystalReportSource1.ReportDocument;
    }
    private ListItem listItem()
    {
        ListItem li = new ListItem();
        li.Text = "---";
        li.Value = "-1";
        return li;
    }
    #endregion
    #region "Public Method"

    #endregion
    #region "Event"
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Page_Init(object sender, EventArgs e)
    {
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        search();

    }
    protected void ddlItemName_DataBound(object sender, EventArgs e)
    {

        ddlItemName.Items.Insert(0,listItem());
    }
    protected void ddlAccountName_DataBound(object sender, EventArgs e)
    {
        ddlAccountName.Items.Insert(0, listItem());
    }
    protected void ddlSalesPerson_DataBound(object sender, EventArgs e)
    {
        ddlSalesPerson.Items.Insert(0, listItem());
    }
    protected void ddlType_DataBound(object sender, EventArgs e)
    {
        ddlType.Items.Insert(0, listItem());
    }

    #endregion

}
